<?php
if($_POST[action]=="save"){

	$fileupload=$_FILES['fileupload']['tmp_name'];
	$fileupload_name=$_FILES['fileupload']['name'];
	$fileupload_size=$_FILES['fileupload']['size'];
	$fileupload_type=$_FILES['fileupload']['type'];
	//check del pic//
	if($_POST[delpic]<>""){
		@unlink("../upload/news/$_POST[delpic]");
		$data = array(
		"image"=>NULL,
		);
		update("tb_news",$data,"id = '$_POST[id]'");
	}
	//end check//
	
	//insert data//
	$data = array(
	"subject"=>($_POST["name"]),
	"detail"=>($_POST["detail"]),
	"date_post"=>date("Y-m-d")
	);
	
	update("tb_news",$data,"id = '$_POST[id]'");
	//end insert//

	//upload picture//
	if ($fileupload) {
		if($_POST[pic]<>""){
		unlink("../upload/news/$_POST[pic]");
		}
		$array_last=explode(".",$fileupload_name);
		$c=count($array_last)-1; 
		$lastname=strtolower($array_last[$c]) ;
		$photoname=date("YmdHis")."_".$_POST[id].".".$lastname;
		$output = "../upload/news/$photoname";
		smart_resize_image($fileupload,400,400,true, $output,false,false);
		$output = "../upload/news/thumb/$photoname";
		smart_resize_image($fileupload,70,70,true, $output,true,false);
		$data = array(
		"image"=>($photoname),
		);
		update("tb_news",$data,"id='".$_POST[id]."'");											
		unlink($fileupload);
	}//end if($fileupload)//
		
	//end upload//
	unset($_POST[action]);
	?>					
	<SCRIPT LANGUAGE="JavaScript">alert("edit content suscessfully");window.location='admin.php?mode=news_list.php';</script>						
	<?php
}//end $_POST[action]//
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
$sql = "select * from tb_news where id = '$_GET[id]'";
$qr = select($sql);
$rs = $qr[0];
?>
<body>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="15" align="left" class="headmenu"><img src="image/bar_left.gif" width="15" height="42" /></td>
    <td align="left" bgcolor="#434343" class="headmenu">ข่าว-&gt;<?php echo $rs['subject']?>-&gt;แก้ไขข้อมูล</td>
    <td width="15" align="left" class="headmenu"><img src="image/bar_right.gif" width="15" height="42" /></td>
  </tr>
</table>

<form action="admin.php?mode=news_edit.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table style="margin-top:20px;" width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
      <td width="20%" align="center" bgcolor="#FFFFFF" class="subject">ชื่อเรื่อง</td>
      <td width="72%" align="left" bgcolor="#FFFFFF"><label>
        <input name="name" type="text" id="name" value="<?php echo $rs['subject']?>" size="30" />
      </label></td>
    </tr>
    <tr>
      <td align="center" valign="top" bgcolor="#FFFFFF" class="subject">รายละเอียด</td>
      <td align="left" bgcolor="#FFFFFF"><label for="detail"></label>
      <textarea name="detail" rows="25" id="detail"><?=$rs['detail']?>
        </textarea></td>
    </tr>
    <tr class="subject">
      <td align="center" bgcolor="#FFFFFF" class="menu">รูปภาพ</td>
      <td align="left" bgcolor="#FFFFFF"><?php if($rs['image']<>""){ ?>
          <img src="../upload/news/<?php echo $rs['image'];?>"/>
          <label>
            <input name="delpic" type="checkbox" id="delpic" value="<?php echo $rs['image']?>" />
          </label>
        ลบรูป <br />
  <?php } ?>
  <label>
    <input name="fileupload" type="file" id="fileupload" />
    ขนาด 337*195 px</label></td>
    </tr>
    <tr>
      <td colspan="2" align="center" bgcolor="#FFFFFF"><label>
        <input type="reset" name="button2" id="button2" value="Reset" />
      </label>
        <label>
        <input type="submit" name="button" id="button" value="Submit" />
        <input name="action" type="hidden" id="action" value="save" />
        <input type="hidden" name="id" id="id" value="<?php echo $rs['id']?>" />
        <input name="pic" type="hidden" id="pic" value="<?php echo $rs['image']?>" />
</label></td>
    </tr>
  </table>
</form>
</body>
</html>
